package com.tongzhan.controller;

import com.tongzhan.base.Return;
import com.tongzhan.entity.User;
import com.tongzhan.entity.religion.Inform;
import com.tongzhan.service.UserService;
import com.tongzhan.service.InformService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Slf4j
@RestController
@RequestMapping("/inform")
public class InformController {

    @Resource
    private UserService userService;

    @Resource
    private InformService informService;

    /**
     * 新增通知/法规接口
     *
     * @param userId 用户id
     * @param type 类型 0-通知 1-法规
     * @param receiveType 接受者类型
     * @param title 主题
     * @param content 内容
     * @param fileUrls 照片编码
     * @param attachment 附件编码
     * @return Return
     */
    @PostMapping("/newInform")
    public Return newInform(String userId, Integer type, String receiveType, String title, String content, String fileUrls, String attachment) {
        if (StringUtils.isEmpty(userId) || StringUtils.isEmpty(title) || type == null) {
            return new Return(Return.MISS_PARAM, "参数缺失");
        }
        if (type == 0 && StringUtils.isEmpty(receiveType)) {
            return new Return(Return.MISS_PARAM, "参数缺失");
        }
        if (title.trim().length() > 1000) {
            return new Return(Return.FAIL, "标题过长");
        }
        if (StringUtils.isNotEmpty(content) && content.trim().length() > 10000) {
            return new Return(Return.FAIL, "内容过长");
        }
        Inform inform = new Inform();
        //用户信息
        User user = userService.selectByUserId(userId);
        if (user != null) {
            //通知编码：用户名+时间
            DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
            String informCode = user.getUserName() + df.format(LocalDateTime.now());
            inform.setInformCode(informCode);
            inform.setUserName(user.getUserName());
            inform.setRealName(user.getRealName());
            inform.setInitiatorType(user.getDutiesCode());
        }
        inform.setTitle(title.trim());
        if (StringUtils.isNotEmpty(content)) {
            inform.setContent(content);
        }
        inform.setRecipientType(receiveType);
        inform.setPicture(fileUrls);
        inform.setAttachment(attachment);
        return informService.newInform(type, inform);
    }

    /**
     * 修改通知/法规接口
     * @param id 通知/法规id
     * @param userId 用户id
     * @param type 类型 0-通知 1-法规
     * @param receiveType 接受者类型
     * @param title 主题
     * @param content 内容
     * @param fileUrls 照片编码
     * @param attachment 附件编码
     * @return Return
     */
    @PostMapping("/editInform")
    public Return editInform(String id, String userId, Integer type, String receiveType, String title, String content, String fileUrls, String attachment) {
        if (StringUtils.isEmpty(userId) || StringUtils.isEmpty(title) || type == null) {
            return new Return(Return.MISS_PARAM, "参数缺失");
        }
        if (type == 0 && StringUtils.isEmpty(receiveType)) {
            return new Return(Return.MISS_PARAM, "参数缺失");
        }
        if (title.trim().length() > 1000) {
            return new Return(Return.FAIL, "标题过长");
        }
        if (StringUtils.isNotEmpty(content) && content.trim().length() > 10000) {
            return new Return(Return.FAIL, "内容过长");
        }
        Inform inform = new Inform();
        //用户信息
        User user = userService.selectByUserId(userId);
        if (user != null) {
            //通知编码：用户名+时间
            DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
            String informCode = user.getUserName() + df.format(LocalDateTime.now());
            inform.setInformCode(informCode);
            inform.setUserName(user.getUserName());
            inform.setRealName(user.getRealName());
            inform.setInitiatorType(user.getDutiesCode());
        }
        inform.setTitle(title.trim());
        if (StringUtils.isNotEmpty(content)) {
            inform.setContent(content);
        }
        inform.setInformCode(id);
        inform.setRecipientType(receiveType);
        inform.setPicture(fileUrls);
        inform.setAttachment(attachment);
        return informService.editInform(type, inform);
    }

    /**
     * 获取通知/法规列表接口
     *
     * @param userId 用户id
     * @param page 页
     * @param size 大小
     * @return Return
     */
    @PostMapping("/getInformList")
    public Return getInformList(String userId, Integer type, Integer page,
                                @RequestParam(required = false, defaultValue = "10") Integer size) {
        if (StringUtils.isEmpty(userId) || type == null || page == null) {
            return new Return(Return.MISS_PARAM, "参数缺失");
        }
        return informService.getInformList(userId, type, page, size);
    }

    /**
     * 通知/法规详情接口
     *
     * @param userId 用户id
     * @param informCode 通知编码
     * @return Return
     */
    @PostMapping("/informDetails")
    public Return informDetails(String userId, String informCode) {
        if (StringUtils.isEmpty(userId) || StringUtils.isEmpty(informCode)) {
            return new Return(Return.MISS_PARAM, "参数缺失");
        }
        return informService.informDetails(userId, informCode);
    }

    /**
     * 删除通知接口
     *
     * @param informCode 通知编码
     * @return Return
     */
    @PostMapping("/deleteInform")
    public Return deleteInform(String informCode) {
        if (StringUtils.isEmpty(informCode)) {
            return new Return(Return.MISS_PARAM, "参数缺失");
        }
        return informService.deleteInform(informCode);
    }
}
